package com.retallife.user.controller;

import com.retallife.user.exception.Result;
import com.retallife.user.pojo.Order.UserAddOrderVo;
import com.retallife.user.pojo.Order.UserOrderNumberVo;
import com.retallife.user.pojo.Order.UserOrderVo;
import com.retallife.user.pojo.Order.UserOrderNameVo;
import com.retallife.user.pojo.User.UserAddFractionVo;
import com.retallife.user.pojo.aliPay.AliPayVo;
import com.retallife.user.service.AliPay.PayService;
import com.retallife.user.service.Goods.GoodsService;
import com.retallife.user.service.Order.OrderService;
import com.retallife.user.service.User.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "用户订单类")
@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET})
public class OrderController {

    @Autowired
    UserService userService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderService orderService;

    @Autowired
    private PayService payService;

    @ApiOperation(value = "用户订单的生成")
    @RequestMapping("AddOrderGoods")
    public Object AddOrderGoods(@RequestBody UserAddOrderVo userAddOrderVo) throws Exception {
        //查询用户地址
        if (userService.findAddressUser(userAddOrderVo.getUserOrderUserName()) == "") {
            return "你的地址出错了";
        }else {
            //设置地址
            String userAddress = userService.findAddressUser(userAddOrderVo.getUserOrderUserName());
            userAddOrderVo.setUserOrderAddress(userAddress);

            //自动添加订单号
            AliPayVo aliPayVo = new AliPayVo();
            Date date = new Date();
            //可以方便地修改日期格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            //转换格式为yyyyMMdd
            String Out_trade_no = dateFormat.format(date);
            //根据时间设置唯一订单号，精确到秒
            aliPayVo.setOut_trade_no(Out_trade_no);
            //设置支付的表单信息
            userAddOrderVo.setUserOrderNumber(Out_trade_no);
            aliPayVo.setUserName(userAddOrderVo.getUserOrderUserName());
            aliPayVo.setTotal_amount(userAddOrderVo.getUserOrderMoney());
            aliPayVo.setSubject(userAddOrderVo.getUserOrderGoodsName());

            String userOrderState = "0";
            userAddOrderVo.setUserOrderState(userOrderState);

            //添加订单表
            orderService.AddOrderGoods(userAddOrderVo);

            //订单完成，用户增加相应的信用分
            UserAddFractionVo userAddFractionVo = new UserAddFractionVo();
            //查询用户的信用分
            int Fraction = userService.findFractionVoUser(userAddOrderVo.getUserOrderUserName());
            //信用分的叠加
            int allFraction = Fraction+(int) Math.round(aliPayVo.getTotal_amount());

            userAddFractionVo.setUserName(aliPayVo.getUserName());
            //Math.round四舍五入，double型直接转int不会四舍五入
            userAddFractionVo.setUserFraction(allFraction);
            userService.addUserFraction(userAddFractionVo);

            return payService.AliPay(aliPayVo);
        }
    }

    @ApiOperation(value = "查询某个用户的订单信息")
    @RequestMapping("FindOrderGoods")
    public List<UserOrderVo> FindOrderGoods(@RequestBody UserOrderNameVo userOrderNameVo) {
        return orderService.FindOrderGoods(userOrderNameVo);
    }

    @ApiOperation(value = "查询某个用户的正在处理的订单信息")
    @RequestMapping("FindOrderGoodsIng")
    public List<UserOrderVo> FindOrderGoodsIng(@RequestBody UserOrderNameVo userOrderNameVo) {
        return orderService.FindOrderGoodsIng(userOrderNameVo);
    }

    @ApiOperation(value = "退款成功的订单")
    @RequestMapping("FindOrderGoodsRefund")
    public List<UserOrderVo> FindOrderGoodsRefund(@RequestBody UserOrderNameVo userOrderNameVo) {
        return orderService.FindOrderGoodsRefund(userOrderNameVo);
    }

    @ApiOperation(value = "申请退款")
    @RequestMapping("UpdateOrderGoods")
    public Result UpdateOrderGoods(@RequestBody UserOrderNumberVo userOrderNumberVo) {
        String userState = "1";
        userOrderNumberVo.setUserOrderState(userState);
        int result = orderService.UpdateOrderGoods(userOrderNumberVo);
        if (result > 0){
            return Result.ok("申请成功");
        }
        return Result.ok("申请失败");
    }
}
